<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 预案步骤管理
 * @package app\script
 * @author LJQ<245369209@qq.com>
 */
class Script extends Api

{

    /**
   *  脚本详情列表
   * @param string 
   * @return Json
   */

  public function scriptList($planId=null,$scriptId=null){
              
             if (empty($planId)) {
                return json(['code'=>0,'msg'=>'没有找到预案']);
             }

             $where['reservePlanId']=['eq',$planId]; 
             if ($scriptId) {
                 $where['id']=['eq',$scriptId]; 
             }
             
    //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      // 脚本详情列表
      $info =collection(model('script')->with('unit')->where($where)->limit($startLimit.','.$limit)->order('stepIndex')->select())->toArray();
    }else{
      //脚本详情列表
      $info =collection(model('script')->with('unit')->where($where)->order('stepIndex')->select())->toArray();
    }
       
    //总数
       $count = model('script')->where($where)->count('id');   
    
              if ($info && $count == 0) {
              return json(['code'=>0,'msg'=>'没有找到脚本详情']);
          } 
        
          if ($count > 0){
            return json(['code'=>1,'msg'=>'脚本详情','count'=>$count,'data'=>$info]);
          }
                return json(['code'=>0,'msg'=>'数据异常']);
       }

      
          //添加脚本详情
     public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('plan/script');
          $result = $model->change($data);
          if (false != $result) {
            setlog('scriptadd','script',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'脚本详情添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'脚本详情添加失败,请检查']);
          }
      }

    

   /**
   * 修改脚本详情
   * @param  string scriptId 脚本详情id,
   * @return Json
   */
    public function edit($data,$scriptId){
        
          if (empty($scriptId)) {
              return json(['code'=>0,'msg'=>'脚本详情不存在,请检查']);
          }
          $model = model('plan/script');     
          if ($model->change($data)) {
             setlog('scriptupdate','script',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'脚本详情修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'脚本详情脚本详情修改失败,请检查']);
            }
    }
    /**
   *  删除脚本详情
   * @param string scriptId 脚本详情id,
   * @param Json
   */
     
    public function del($scriptId) {
        $scriptId = input('scriptId',0,'intval');
      
        $model = model('plan/script');
        if (empty ( $scriptId )||empty(($model->where(array('id'=>$scriptId))->find()))) {
            return  json(['code'=>0,'msg'=>'脚本详情id不存在,请检查']);
        }
        if($model->destroy(['id'=>$scriptId]) !== FALSE){
           setlog('scriptdel','script',session('user_auth.id'));
           return json(['code'=>1,'msg'=>'脚本详情删除成功']);
       }else{
           return $this->json(['code'=>0,'msg'=>'脚本详情删除失败,请检查']);
       }
    }


       /**
   *   获得企业列表
   * @return Json
   */
      public function getEnterprise() {
           $enterprise = model('basic/Enterprise');
           $enterprsies = collection($enterprise->field('id,fullName')->order('id')->select())->toArray();
           $enterprsies[] = array_unshift($enterprsies, ['id'=>0 , 'fullName'=>'管委会']);
           return json(['code'=>1,'msg'=>'企业列表','data'=>$enterprsies]);
    }

}
